<?php

namespace app\api\controller;

use app\api\model\Admin as AdminModel;
use app\api\model\Feedback as FeedbackModel;
use think\Db;
use think\Request;
use app\reserve\model\AdminGroup;

class Admin extends Base{


    /**
    * Notes: 管理员登录
    * Create Date: 2018/10/2
    * Create Time: 15:24
    * Create by :lijie
    * Param :
    *   $account        账号
    *   $password       密码
    */
    public function login(Request $request){
        $admin    = new AdminModel();

        // 接收数据并验证
        $req = $request->post();
        $res_validate = $this->validate($req,"AdminValidate.login");
        if($res_validate !== true){
            return apiResponse(201,$res_validate,'');
        }

        // 用户是否存在
        $w['account']  = $req['account'];
        $w['status']   = 0;
        $adminInfo = $admin->findAdmin($w);
        if($adminInfo){
            //密码是否正确
            if($adminInfo['password'] == md5($req['password'])){
                $save['last_login_time']   = time();
                $save['last_login_ip']     = $_SERVER['REMOTE_ADDR'];
                $w['id']                   = $adminInfo['id'];
                $res = $admin->editAdmin($w,$save);
                if($res){
                    $auth = new AdminGroup();
                    $authList = $auth->getDetailsById($adminInfo['group_id']);
                    return apiResponse('200','登录成功',$authList);
                }else {
                    return apiResponse('201','登录失败','');
                }
            }else{
                return apiResponse('201','密码错误','');
            }
        }else {
            return apiResponse('201','用户不存在','');
        }
    }
    /*
    * Notes: 用户反馈信息列表
    * Create Date: 2018/10/8
    * Create Time: 下午5:07
    * Create by :张福来
    * param :
    */
    public function feedbackList(Request $request){
        $admin    = new FeedbackModel();
        $postData = $request->post();
        $check = $this->validate($postData,"AdminValidate.feedbackList");
        if($check !== true){
            return apiResponse(201,$check,"");
        }
        if(empty($postData["listNum"])){
            $listNum = 10;
        }else{
            $listNum = $postData["listNum"];
        }
        if(empty($postData["pageNum"])){
            $pageNum = 1;
        }else{
            $pageNum = $postData["pageNum"];
        }
        if(!empty($postData["mix_id"])){
            $w["a.mix_id"] = $postData["mix_id"];
        }
        if(!empty($postData["type"])){
            $w["a.type"] = $postData["type"];
        }
        $type = $postData["type"];
        if(!empty($postData["name"])){
            $w['a.name'] = array('LIKE','%'.trim($postData["name"]).'%');
        }
        if(!empty($postData["status"])){
            $w["a.status"] = $postData["status"];
        }else{
            $w["a.status"] = array('NEQ',9);
        }

        if(!empty($postData["start_time"]) && !empty($postData["end_time"])){
            $start_time = $postData["start_time"];
            $end_time = $postData["end_time"];
            $w['a.ctime'] = array(array('EGT',strtotime($start_time)),array('ELT',strtotime($end_time)),'and');
        }
        $result = $admin->feedbackList($pageNum,$listNum,$w,$type);
        if (!empty($result["list"])){
            return apiResponse(200,"查询成功",$result);
        }else{
            return apiResponse(200,"数据为空",$result);
        }
    }
    /*
    * Notes:  解决反馈信息
    * Create Date: 2018/10/8
    * Create Time: 下午5:41
    * Create by :张福来
    * param :
    */
    public function updateFeedback(Request $request){
        $admin    = new FeedbackModel();
        $postData = $request->post();
        $check = $this->validate($postData,"AdminValidate.updateFeedback");
        if($check !== true){
            return apiResponse(201,$check,"");
        }
        $w["id"] = $postData["id"];
        $c["utime"] = time();
        if(!empty($postData["type"])){
            $c["status"] = 1;
        }else{
            $c["customer"] = $postData["customer"];
            $c["c_content"] = $postData["c_content"];
            $c["status"] = 2;
        }
        $result =  $admin-> updateFeedback($w,$c);
        if($result){
            return apiResponse(200,"解决成功",$result);
        }else{
            return apiResponse(201,"解决失败",$result);
        }
    }

    /**
     * Notes: 管理员修改密码
     * Create Date: 2018/10/2
     * Create Time: 15:24
     * Create by :lijie
     * Param :
     *   id        账号
     *   $password   旧密码
     *   $new_password   新密码
     */
    public function editPassword(Request $request){
        $admin = new AdminModel();

        // 获取数据并验证
        $param = $request->post();
        $resVali = $this->validate($param,"AdminValidate.editPassword");
        if($resVali !== true){
            return apiResponse(201,$resVali,'');
        }

        // 判断账号是否存在
        $adminInfo = $admin->findAdmin(['id'=>$param['id']]);
        if($adminInfo){
            // 判断旧密码是否正确
            if($adminInfo['password'] != md5($param['password'])){
                return apiResponse(201,'旧密码错误，请重新输入','');
            }
            // 修改密码
            $w['id']          = $adminInfo['id'];
            $save['password'] = md5($param['new_password']);
            $res = $admin->editAdmin($w,$save);
            if($res){
                return apiResponse(200,'密码修改成功','');
            }else {
                return apiResponse(201,'密码修改失败','');
            }
        }else {
            return apiResponse(201,'该用户不存在','');
        }
    }
    /*
    * Notes: 修改管理员
    * Create Date: 2018/10/23
    * Create Time: 下午4:01
    * Create by :张福来
    * param :
    */
    public function eiditAdmin(Request $request){
        $admin = new AdminModel();

        // 获取数据并验证
        $param = $request->post();
        $resVali = $this->validate($param,"AdminValidate.eiditAdmin");
        if($resVali !== true){
            return apiResponse(201,$resVali,'');
        }

        // 判断账号是否存在
        $adminInfo = $admin->findAdmin(['id'=>$param['id']]);
        if($adminInfo){
            // 判断旧密码是否正确
            if($adminInfo['password'] != md5($param['password'])){
                return apiResponse(201,'旧密码错误，请重新输入','');
            }
            // 修改密码
            $w['id']          = $adminInfo['id'];
            $save['password'] = md5($param['new_password']);
            $save['group_id'] = $param['group_id'];
            $save['account'] = $param['account'];
            $res = $admin->editAdmin($w,$save);
            if($res){
                return apiResponse(200,'密码修改成功','');
            }else {
                return apiResponse(201,'密码修改失败','');
            }
        }else {
            return apiResponse(201,'该用户不存在','');
        }
    }
    /**
     * Notes: 添加管理员
     * Create Date: 2018/10/16
     * Create Time: 15:24
     * Create by :lijie
     * Param :
     *   $account    账号
     *   $password   密码
     */
    public function addAdmin(Request $request){
        $admin = new AdminModel();

        // 验证功能
        $param = $request->post();
        $resVali = $this->validate($param,"AdminValidate.addAdmin");
        if($resVali !==true){
            return apiResponse(201,$resVali,'');
        }

        // 账号是否存在
        $adminInfo = $admin->findAdmin(['account'=>$param['account']]);
        if($adminInfo){
            return apiResponse(201,"该账号已经存在",'');
        }

        $addArr['account']  = $param['account'];
        $addArr['group_id'] = $param['group_id'];
        $addArr['password'] = md5($param['password']);
        $addArr['ctime']    = time();
        $addArr['utime']    = time();
        $addArr['status']   = 0;
        $res = $admin->addAdmin($addArr);
        if($res){
            return apiResponse(200,'添加成功','');
        }else {
            return apiResponse(201,'添加失败','');
        }
    }

    /**
     * Notes: 管理员列表
     * Create Date: 2018/10/17
     * Create by :lijie
     * Param :
     *   $account    账号
     *   $password   密码
     */
    public function adminList(Request $request){
        $admin    = new AdminModel();
        //  验证
        $param = $request->post();
        $resVali = $this->validate($param,"AdminValidate.adminList");
        if($resVali !== true){
            return apiResponse(201,$resVali,'');
        }
        // 条件筛选
        if(!empty($param['account'])){
            $w['account'] = $param['account'];
        }
        if(!empty($param['id'])){
            $w['id'] = $param['id'];
        }
        if(!empty($param['group_id'])){
            $w['group_id'] = $param['group_id'];
        }
        $w['status'] = 0;
        // 分页
        $page = $param['pagenum'];
        if(!empty($param['listNum'])){
            $pagelist = $param['listNum'];
        }else{
            $pagelist = 10;
        }

        $list = $admin->selectAdmin($w,$page,$pagelist);
        $total = Db::name('admin')->where($w)->count();
        $data['total']= $total;
        if($list){
            // 判断最后登录时间
            foreach ($list as $key=>$val) {
                if($list[$key]['last_login_time'] == 0){
                    $list[$key]['last_login_time'] = '暂无登录';
                }else {
                    $list[$key]['last_login_time']  = date("Y-m-d H:i:s",$list[$key]['last_login_time']);
                }
            }
            $data['list'] = $list;
            return apiResponse(200,"获取成功",$data);
        }else {
            return apiResponse(201,"获取失败",$data);
        }
    }

    /**
     * Notes: 管理员添加权限
     * Create Date: 2018/10/10
     * Create by :lijie
     *
     * param : id   银行卡id
     * param : group_id
     */
    public function addPower()
    {
        $id       = input('post.id');
        $group_id = input('post.group_id');
        if (empty($id) && empty($group_id)){
            return apiResponse(201,'参数错误','');
        }
        //
        $admin    = new AdminModel();
        $where['id']    = $id;
        $where['status'] = array("NEQ",9);
        $save['group_id'] = $group_id;
        $save['utime']    = time();
        $res = $admin->editAdmin($where,$save);
        if($res){
            return apiResponse(200,'添加成功',$res);
        }else {
            return apiResponse(201,"添加失败",$res);
        }
    }
}
